<template>
  <div>
    <div>
      部门管理
    </div>
    <div class="search-box">
      <el-form ref="filter" label-width="70px" label-position="left" :model="filter" style="display: flex">
        <el-form-item label="部门名称">
          <el-input v-model="filter.institutionName" size="small" style="width:200px;margin-right:20px;"></el-input>
        </el-form-item>
      </el-form>
      <el-button size="small" type="warning" @click="search">查询</el-button>
      <el-button size="small" type="">重置</el-button>
      <el-button size="small" type="primary" @click="addUser">新增</el-button>
    </div>
    <!--部门列表-->
    <div style="overflow:hidden;border-top:1px solid #ccc">
      <el-table :data="institutionList" stripe style="width:100%" v-loading="loading">
        <el-table-column prop="institutionName" label="部门" min-width="200"></el-table-column>
        <el-table-column prop="institutionLevelName" label="机构级别" min-width="200"></el-table-column>
        <el-table-column label="上级机构" min-width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.parentName == '0'"></span>
            <span v-else>{{scope.row.parentName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="branchAddress" label="地址" min-width="200"></el-table-column>
        <el-table-column label="操作" min-width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="editUser(scope.$index, scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页功能-->
      <el-pagination
        style="margin-top:20px;justify-content:flex-end;display:flex;"
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page.sync="currentPage"
        :page-size="10"
        layout="prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!--新增弹窗-->
    <el-dialog title="新增部门" :visible.sync="addDialogVisible" width="30%" :close-on-click-modal="false">
      <el-form :model="addForm" ref="addForm" :rules="addRules" label-width="80px" >
        <el-form-item label="部门名称" prop="institutionName">
          <el-input v-model="addForm.institutionName" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="parentId">
          <el-select v-model="addForm.parentId" placeholder="请选择上级部门">
            <el-option v-for="item in institutionIdArr"
                       :key="item.institutionId"
                       :label="item.institutionName"
                       :value="item.institutionId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构级别" prop="institutionLevel">
          <el-select v-model="addForm.institutionLevel" placeholder="请选择机构级别">
            <el-option v-for="item in institutionLevelArr"
                       :key="item.level"
                       :label="item.name"
                       :value="item.level">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门地址" prop="branchAddress">
          <el-input v-model="addForm.branchAddress" placeholder="请输入部门地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="addCancel">取 消</el-button>
        <el-button size="small" type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!--修改弹窗-->
    <el-dialog title="修改部门" :visible.sync="editDialogVisible" width="30%" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="editRules" label-width="80px" >
        <el-form-item label="部门名称" prop="institutionName">
          <el-input v-model="editForm.institutionName" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="parentId">
          <el-select v-model="editForm.parentId" placeholder="请选择上级部门">
            <el-option v-for="item in institutionIdArr"
                       :key="item.institutionId"
                       :label="item.institutionName"
                       :value="item.institutionId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构级别" prop="institutionLevel">
          <el-select v-model="editForm.institutionLevel" placeholder="请选择机构级别">
            <el-option v-for="item in institutionLevelArr"
                       :key="item.level"
                       :label="item.name"
                       :value="item.level">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门地址" prop="branchAddress">
          <el-input v-model="editForm.branchAddress" placeholder="请输入部门地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="editCancel">取 消</el-button>
        <el-button size="small" type="primary" @click="editSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import server from '../../services/index'
  export default {
    components: {},
    data () {
      return {
        filter:{
          title:"",
        },
        institutionList: [],
        loading: false,
        total: 0,
        currentPage: 1,
        pageSize: 10,
        addDialogVisible: false,
        editDialogVisible: false,
        addForm:{
          institutionName:'',
          parentId:'',
          institutionLevel:'',
          branchAddress:'',
        },
        addRules:{
          institutionName:[
            { required: true, message: '请输入部门名称！', trigger: ['blur', 'change'] },
          ],
          parentId:[
            { required: true, message: '请选择上级部门！', trigger: ['blur', 'change'] },
          ],
          institutionLevel:[
            { required: true, message: '请选择机构级别！', trigger: ['blur', 'change'] },
          ],
          branchAddress:[
            { required: true, message: '请输入部门地址！', trigger: ['blur', 'change'] },
          ]
        },
        editForm:{
          institutionName:'',
          parentId:'',
          institutionLevel:'',
          branchAddress:'',
        },
        editRules:{
          institutionName:[
            { required: true, message: '请输入部门名称！', trigger: ['blur', 'change'] },
          ],
          parentId:[
            { required: true, message: '请选择上级部门！', trigger: ['blur', 'change'] },
          ],
          institutionLevel:[
            { required: true, message: '请选择机构级别！', trigger: ['blur', 'change'] },
          ],
          branchAddress:[
            { required: true, message: '请输入部门地址！', trigger: ['blur', 'change'] },
          ]
        },
        institutionIdArr:[{
          institutionId:'0',
          institutionName:'无'
        }],
        institutionLevelArr:[{
          level:'1',
          name:'总行',
        },{
          level:'2',
          name:'省行',
        },{
          level:'3',
          name:'支行',
        },{
          level:'4',
          name:'网点',
        }]
      }
    },
    methods:{
      getListData(){
        this.loading = true;
        let params = {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        }
        $.ajax({
          url:server.getinstitutionList.url,
          type:'post',
          data:params,
          dataType:'json',
          success:res => {
            this.institutionIdArr = [{
              institutionId:'0',
              institutionName:'无'
            }];
            this.loading = false;
            this.institutionList = res.result.institutionList;
            this.total = res.length;
            this.institutionList.forEach(item=>{
              this.institutionIdArr.push({
                institutionId:item.institutionId,
                institutionName:item.institutionName
              })
            })
          },
          error:err =>{
            this.$message({
              message: err.responseJSON.error,
              type: 'error'
            });
          }
        });
      },
      sizeChange(val){
        this.pageSize = val;
        this.getListData();
      },
      currentChange(val){
        this.currentPage = val;
        this.getListData();
      },
      addUser(){
        this.addDialogVisible = true;
      },
      addSubmit(){
        this.$refs.addForm.validate((valid) => {
          if(valid){
            this.$confirm('确认添加新部门？').then(() => {
              var parentName = '';
              this.institutionIdArr.forEach(item=>{
                if(item.institutionId == this.addForm.parentId){
                  parentName = item.institutionName
                }
              })
              let params = {
                institutionName:this.addForm.institutionName,
                parentId:this.addForm.parentId,
                parentName:parentName,
                institutionLevel:this.addForm.institutionLevel,
                branchAddress:this.addForm.branchAddress,
              }
              console.log(params)
              $.ajax({
                url: server.insertInstitution.url,
                type: 'post',
                data: params,
                dataType: 'json',
                success: res => {
                  if(res.code == 1){
                    this.$message({
                      message: '新增成功！',
                      type: 'success'
                    });
                    this.addDialogVisible = false;
                    this.$refs.addForm.resetFields();
                    this.getListData();
                  }
                },
                error: err => {
                  this.$message({
                    message: err.responseJSON.error,
                    type: 'error'
                  });
                }
              })
            }).catch(() => {});
          }else{
            return false;
          }
        });
      },
      addCancel(){
        this.addDialogVisible = false;
        this.$refs.addForm.resetFields();
      },
      editUser(index, row){
        this.editDialogVisible = true;
        this.editForm.institutionId = row.institutionId;
        this.editForm.institutionName = row.institutionName;
        this.editForm.parentId = row.parentId;
        this.editForm.institutionLevel = row.institutionLevel;
        this.editForm.branchAddress =row.branchAddress;
      },
      editSubmit(){
        this.$refs.editForm.validate((valid) => {
          if(valid){
            this.$confirm('确认修改部门？').then(() => {
              var parentName = '';
              this.institutionIdArr.forEach(item=>{
                if(item.institutionId == this.addForm.parentId){
                  parentName = item.institutionName
                }
              })
              let params = {
                institutionId:this.editForm.institutionId,
                institutionName:this.editForm.institutionName,
                parentId:this.editForm.parentId,
                parentName:parentName,
                institutionLevel:this.editForm.institutionLevel,
                branchAddress:this.editForm.branchAddress,
              }
              console.log(params)
              $.ajax({
                url: server.insertInstitution.url,
                type: 'post',
                data: params,
                dataType: 'json',
                success: res => {
                  if(res.code == 1){
                    this.$message({
                      message: '修改成功！',
                      type: 'success'
                    });
                    this.addDialogVisible = false;
                    this.$refs.editForm.resetFields();
                    this.getListData();
                  }
                },
                error: err => {
                  this.$message({
                    message: err.responseJSON.error,
                    type: 'error'
                  });
                }
              })
            }).catch(() => {});
          }else{
            return false;
          }
        });
      },
      editCancel(){
        this.$refs.editForm.resetFields();
        this.editDialogVisible = false;
      },
      deleteUser(index, row){
        this.$confirm('确认删除该部门？').then(() => {
          $.ajax({
            url:server.deleteInstitution.url,
            type:'get',
            data:{
              institutionId:row.institutionId
            },
            dataType:'json',
            success:res => {
              if(res.code == 1){
                this.$message({
                  message: '删除成功！',
                  type: 'success'
                });
                this.getListData();
              }
            },
            error:err =>{
              this.$message({
                message: err.responseJSON.error,
                type: 'error'
              });
            }
          });
        }).catch(() => {});
      },
      search(){
        this.loading = true;
        let params = {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        }
        if(this.filter.institutionName) params.institutionName = this.filter.institutionName;
        $.ajax({
          url:server.searchInstitution.url,
          type:'post',
          data:params,
          dataType:'json',
          success:res => {
            if(res.code == 1){
              this.loading = false;
              if(!res.result){
                this.tradeList = [];
              }else{
                this.tradeList = res.result.tradeList;
                this.total = res.result.total;
              }
            }
          },
          error:err =>{
            this.$message({
              message: err.responseJSON.error,
              type: 'error'
            });
          }
        });
      },
    },
    mounted(){
      if(!window.sessionStorage.getItem("token"))this.$router.push({path: '/'});
      this.getListData();
    }
  }
</script>

<style scoped>
  .search-box{
    margin: 20px 0;
  }
</style>
